﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Dialog
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnFile_Click(object sender, EventArgs e)
        {
            //点击弹出对话框
            var dialog = new OpenFileDialog();
            //设置对话框属性
            dialog.Title = "请选择要打开的文本文件";//对话框标题
            dialog.Multiselect = true;//支持对话框可以多选
            dialog.InitialDirectory = @"E:\1Rice\12B";//打开的默认目录
            dialog.Filter = "文本文件|*.txt";//文件类型

            //展开对话框
            dialog.ShowDialog();

            //获得选中文件的路径
            var paths = dialog.FileNames;
            //判断如果选择文件的路径为空
            if (paths.Length == 0)
            {
                MessageBox.Show("请选择一个文件！");
                return;
            }
            //读取二进制文件，显示在文本框
            textBox1.Text = string.Empty;//清空文本框
            foreach(var path in paths)
            {
                //读取文件
                using (FileStream fsRead = new FileStream(path,FileMode.OpenOrCreate,FileAccess.Read))
                {
                    //读取文件不能超过5M（定义范围）
                    byte[] buffer = new byte[1024 * 1024 * 5];
                    //读取当前文件的有效字节数
                    int r = fsRead.Read(buffer, 0, buffer.Length);
                    //将读出的二进制字节流转换成Utf8格式的字符串
                    string str = Encoding.UTF8.GetString(buffer, 0, r)+"\r\n\r\n";
                    //关闭文件流
                    fsRead.Close();
                    textBox1.Text = str;
                }
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            //通过对话框选择保存的路径
            //点击弹出的对话框
            var dialog = new SaveFileDialog();
            //设置对话框属性
            dialog.Title = "请选择要打开的文本文件";//对话框标题
            dialog.InitialDirectory = @"E:\1Rice\12B";//打开的默认目录
            dialog.Filter = "文本文件|*.txt";//文件类型

            //展开对话框
            dialog.ShowDialog();

            //二进制文件的写入
            var path = dialog.FileName;

            //判断如果选择文件的路径为空
            if (path.Length == 0)
            {
                MessageBox.Show("请选择一个文件！");
                return;
            }
            if (path != "")
            {
                using (FileStream fsWrite = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
                {
                    //把字符串转换为二进制
                    byte[] buffer = Encoding.UTF8.GetBytes(textBox1.Text);
                    //读取当前文件的有效字节数
                    fsWrite.Write(buffer, 0, buffer.Length);
                }
                MessageBox.Show("保存成功！");
            }

        }

        private void btnSize_Click(object sender, EventArgs e)
        {
            //选择字体
            //打开字体对话框
            var fd = new FontDialog();
            fd.ShowDialog();

            //把选好的字体赋值给文本框
            textBox1.Font = fd.Font;
        }

        private void btnColor_Click(object sender, EventArgs e)
        {
            //选择颜色
            //打开颜色对话框
            var cd = new ColorDialog();
            cd.ShowDialog();

            //把选择好的颜色赋值给文本框
            textBox1.ForeColor = cd.Color;
        }
    }
}
